package com.shop.www.controller;

import com.google.gson.Gson;
import com.shop.www.model.Cart;
import com.shop.www.model.CartItem;
import com.shop.www.model.Commodity;
import com.shop.www.model.User;
import com.shop.www.service.CartItemservice;
import com.shop.www.service.Impl.CartItemserviceImpl;

import javax.servlet.http.HttpSession;
import java.util.Map;

public class Cartcontroller {//购物车页面
private CartItemservice cartItemservice;

    //在跳转到购物车时更新session中的数据
    public String index(HttpSession session){
        User user =(User)session.getAttribute("loginuser");
        Cart cart = cartItemservice.getCart(user);
        user.setCart(cart);
        session.setAttribute("loginuser",user);
        return "cart";
    }
    public String cartInfo(HttpSession session){//购物车页面(Vue)
            User user =(User)session.getAttribute("loginuser");
            Cart cart = cartItemservice.getCart(user);
            Gson gson = new Gson();
            String cartJsonStr = gson.toJson(cart);
            return "json:"+cartJsonStr;
    }

    public String getkind(HttpSession session){//商城首页的购物车角标(Vue)
        User user =(User)session.getAttribute("loginuser");
        Cart cart = cartItemservice.getCart(user);
        Integer kind = cart.getKind();
        Gson gson = new Gson();
        String cartJsonStr = gson.toJson(kind);
        return "json:"+cartJsonStr;
    }

    public  String addcart(Integer commodity_ID, HttpSession session){
        User user = (User)session.getAttribute("loginuser");
        CartItem cartItem = new CartItem(commodity_ID,1,user.getUser_ID());
        //将商品加入购物车，如果已有该商品则数量+1，否则创建一个新cartitem
        cartItemservice.addorupdateCartItem(cartItem,user.getCart());
        Cart cart = cartItemservice.getCart(user);
        user.setCart(cart);
        session.setAttribute("loginuser",user);
        /*return "redirect:cart.do";*/
        return "";
    }

    public  String addcartThyme(Integer commodity_ID, HttpSession session){
        User user = (User)session.getAttribute("loginuser");
        CartItem cartItem = new CartItem(commodity_ID,1,user.getUser_ID());
        //将商品加入购物车，如果已有该商品则数量+1，否则创建一个新cartitem
        cartItemservice.addorupdateCartItem(cartItem,user.getCart());
        return "redirect:cart.do";
    }

    public String editcart(Integer cartItem_ID,Integer buyCount){//购物车内修改商品数量
        cartItemservice.updateCartItem(new CartItem(cartItem_ID,buyCount));
        return "";
    }

    public String delcart(Integer cartItem_ID,HttpSession session){//删除购物车内商品
        cartItemservice.delCartItem(new CartItem(cartItem_ID));
        User user = (User) session.getAttribute("loginuser");
        Cart cart = user.getCart();
        Map<Integer,CartItem> cartItemMap= cart.getCartItemMap();
        cart.setLock(0);
        for(CartItem cartItem : cart.getCartItemMap().values()){
            if(cartItem.getCommodity().getState()!=("1")){
                cart.setLock(1);
            }
        }
        session.setAttribute("loginuser",user);
        return "";
    }

    public String delallcart(HttpSession session){//删除购物车内所有商品
        User user =(User) session.getAttribute("loginuser");
        Cart cart = cartItemservice.getCart(user);
        for(CartItem cartItem : cart.getCartItemMap().values()){
            cartItemservice.delCartItem(cartItem);
        }
        cart.setLock(0);
        return "";
    }
}
